// Example user-defined function, documentation: https://goo.gl/6KR8O0
// Sample SQL: SELECT outputA, outputB FROM (passthrough(SELECT "abc" AS inputA, "def" AS inputB))
/*
function passthroughExample(row, emit) {
emit({outputA: row.inputA, outputB: row.inputB});
}
bigquery.defineFunction(
'passthrough', // UDF의 이름
['inputA', 'inputB'], // input columns의 이름입니다
[{'name': 'outputA', 'type': 'string'}, // Output schema를 설정해주세요
{'name': 'outputB', 'type': 'string'}],
passthroughExample // Reference to JavaScript UDF
);
*/
// UDF definition
function function_name(row, emit) {
emit(output data);
}
// Helper function with error handling
function helpter_function_name(s) {
try {
return decodeURI(s);
} catch (ex) {
return s;
}
}
// UDF registration
bigquery.defineFunction(
'function_name',
['input column'],
// JSON representation of the output schema
[{output_key: 'output_value'}],
function_name
);
// UDF definition
function urlDecode(row, emit) {
emit({title: decodeHelper(row.title),
requests: row.num_requests});
}
// Helper function with error handling
function decodeHelper(s) {
try {
return decodeURI(s);
} catch (ex) {
return s;
}
}
// UDF registration
bigquery.defineFunction(
'urlDecode', // Name used to call the function from SQL
['title', 'num_requests'], // Input column names
// JSON representation of the output schema
[{name: 'title', type: 'string'},
{name: 'requests', type: 'integer'}],
urlDecode // The function reference
);
UDF Editor에 위 코드를 넣고 Query Editor로 넘어오고 아래와 같은 쿼리를 작성했습니다
SELECT requests, title
FROM
urlDecode(
SELECT
title, sum(requests) AS num_requests
FROM
[fh-bigquery:wikipedia.pagecounts_201504]
WHERE language = 'fr'
GROUP EACH BY title
)
WHERE title LIKE '%ç%'
ORDER BY requests DESC
LIMIT 100
Inline UDF라고 표시되며, 데이터의 양은 300GB인데 쿼리 비용은 약 1.5달러입니다..! (BQ Mate라는 크롬 확장 프로그램을 설치하면 예상 가격이 나옵니다)
SELECT outputA
FROM JS(
// input table
(
SELECT text2 as inputA
FROM
(SELECT 'mikhail' AS text2),
(SELECT 'mike' AS text2),
(SELECT 'michael' AS text2),
(SELECT 'javier' AS text2),
(SELECT 'thomas' AS text2)
)
// input columns
, inputA
// output schema
, "[{name: 'outputA', type:'string'}]"
// function
, "function(r, emit) {
emit({
outputA: 'XX ' + r.inputA + ' XX'
});
}"
)
outputA
XX mikhail XX
XX mike XX
XX michael XX
XX javier XX
XX thomas XX